#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <cstdlib>
#include <iostream>
#include "sdlglutils.h"


using namespace std;
GLuint texture1;
enum etatCouleur{ROUGE,VERT,BLEU,JAUNE,BLANC};


void Dessiner(etatCouleur etatC);

double angleZ = 0;
double angleX = 0;
double angleY = 0;
int main(int argc, char *argv[])
{
    SDL_Event event;
    int FoVy=64;
    etatCouleur etatC=ROUGE;
    //texture1 = loadTexture("metal.jpg",true);
    SDL_Init(SDL_INIT_VIDEO);
    atexit(SDL_Quit);
    SDL_WM_SetCaption("SDL GL Application", NULL);
    SDL_SetVideoMode(1024, 768, 32, SDL_OPENGL);
    glEnable(GL_TEXTURE_2D);
   
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective(30,(double)640/480,1,1000);

    glEnable(GL_DEPTH_TEST);

    Dessiner(etatC);

    Uint32 last_time = SDL_GetTicks();
    Uint32 current_time,ellapsed_time;
    Uint32 start_time;

    for (;;)
    {
        start_time = SDL_GetTicks();
        while (SDL_PollEvent(&event))
	  {
	    
            switch(event.type)
	      {
	      case SDL_QUIT:
		exit(0);break;
	      case  SDL_KEYDOWN:{
		switch(event.key.keysym.sym)
		  {
		  case SDLK_UP:{
		    if(FoVy==150) FoVy=10;
		    else FoVy+=10;
		    glMatrixMode( GL_PROJECTION );
		    glLoadIdentity();
		    gluPerspective(FoVy,(double)640/480,1,1000);
		    cout<<"Appui sur UP"<<endl;
		  }
		    break;
		  }
	      }
	      }
	  }
	
	current_time = SDL_GetTicks();
	ellapsed_time = current_time - last_time;
        last_time = current_time;
	
        angleZ -= 0.005  * ellapsed_time;
	angleX += 0.025 * ellapsed_time;
	angleY += 0.125 * ellapsed_time;
	//FoVy+=1;
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	gluPerspective(FoVy,(double)640/480,1,1000);
        Dessiner(etatC);
	
        ellapsed_time = SDL_GetTicks() - start_time;
        if (ellapsed_time < 10)
	  {
	    SDL_Delay(10 - ellapsed_time);
	    start_time=SDL_GetTicks();
	  }
	
    }
    
    return 0;
}

void Dessiner(etatCouleur etatC)
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  
  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity( );
  
  gluLookAt(3,4,2,0,0,0,0,0,1);
  for (int i=0;i<5;i++){
    glRotated(angleZ,0,0,1);
    glRotated(angleX,1,0,0);
    glRotated(angleY,0,1,0);
    
    glBegin(GL_LINE_LOOP);
    glBindTexture(GL_TEXTURE_2D, texture1);
    
    if (etatC==ROUGE){
      glColor3ub(255,0,0);
    }
    if (etatC==VERT){
      glColor3ub(0,255,0);
    }
    if (etatC==BLEU){
      glColor3ub(0,0,255);
    }
    if (etatC==JAUNE){
      glColor3ub(255,255,0);
    }
    if (etatC==BLANC){
      glColor3ub(255,255,255);
    }
    // 1°Face
    // glColor3ub(255,0,0);
    glVertex3d(0,0,0);//sommet haut du tetraedre
    //glColor3ub(0,255,0);
    glVertex3d(-1,-1,1);
    //glColor3ub(0,0,255);
    glVertex3d(1,-1,1);
    //2°Facegl
    //glColor3ub(0,255,0);
    glVertex3d(0,0,0);
    //glColor3ub(255,0,0);
    glVertex3d(1,-1,1);
    //glColor3ub(0,0,255);
    glVertex3d(1,-1,-1);
    //3°Face
    //glColor3ub(0,0,255);
    glVertex3d(0,0,0);
    //glColor3ub(255,0,0);
    glVertex3d(1,-1,-1);
    //glColor3ub(0,255,0);
    glVertex3d(-1,-1,-1);
    //4°Face
    //glColor3ub(255,255,0);
    glVertex3d(0,0,0);
    //glColor3ub(255,0,0);
    glVertex3d(-1,-1,-1);
    //glColor3ub(0,0,255);
    glVertex3d(-1,-1,1);

    
    etatC=(etatCouleur)(((int)etatC)%5+1);

    glEnd();
  }
  glFlush();
  SDL_GL_SwapBuffers();
}
